"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 数据库系统
Case Name   : 触发SIGFPE信号量，检查是否走UCE检测
Create At   : 2021/05/11
@zou_jialiang0505328126
Description :
    1. 触发SIGFPE
Expect      :
    1.数据库停止，未触发UCE故障探测
History     :
    Modified by @wan005:修改用例编号
    Modified by @wan005 2021/07/06:修改log查询方式，规避测试环境时间跳转问题
    Modified by @wan005 2021/07/06:修改log查询方式
    Modified by @wan005 2021/11/1:适配最新代码，数据库异常停止后集群状态为unavailable
"""


import unittest
import os
from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.CommonSH import RestartDbCluster


class Uceclass(unittest.TestCase):
    db_primary_user_node = Node(node='PrimaryDbUser')
    db_primary_root_node = Node(node='PrimaryRoot')
    logger = Logger()
    common = Common()
    commonsh = CommonSH()
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.logger.info("-----------this is setup-----------")
        self.logger.info('--Opengauss_Reliability_OS_UCE_Case013.py start--')
        self.pg_log = os.path.join(
            macro.PG_LOG_PATH, macro.DN_NODE_NAME.split('/')[0])

    def test_stop_immediate(self):
        self.logger.info('-----------kill -8------------------')
        self.common.kill_pid(self.db_primary_root_node, '8')
        result = self.commonsh.getDbClusterStatus('detail')
        self.assertIn("Unavailable", result)

        self.logger.info('--------------check pg_log-----------------')
        cmd = f'find {self.pg_log} -iname "post*" -mmin -2 | xargs ls -lta'
        self.logger.info(cmd)
        result = self.db_primary_root_node.sh(cmd).result()
        self.logger.info(result)
        self.current_log = result.splitlines()[0].split(" ")[-1]
        cmd = f'cat {self.current_log} | grep "SIGBUS signal received"'
        self.logger.info(cmd)
        result = self.db_primary_root_node.sh(cmd).result()
        self.logger.info(result)
        self.assertNotIn('SIGBUS signal received', result)

        cmd = f'cat {self.current_log} | ' \
            f'grep "FATAL:  floating-point exception"'
        self.logger.info(cmd)
        result = self.db_primary_root_node.sh(cmd).result()
        self.logger.info(result)
        self.assertIn('FATAL:  floating-point exception', result)

    def tearDown(self):
        self.logger.info('----------this is tearDown---------')
        self.logger.info('restart database')
        self.commonsh.startDbCluster()
        self.logger.info('--Opengauss_Reliability_OS_UCE_Case013.py end--')
